// index.ts

import { HOST_URL } from "../../config/index"

// 获取应用实例
// const app = getApp<IAppOption>()

Component({
  data: {
    top: 0,
    status: 0,
    question: '',
    showInput: false,
    answer: '',
    moreAnswer: '',
    id: 0,
  },
  lifetimes: {
    attached() {
      const { statusBarHeight } = wx.getWindowInfo()
      this.setData({
        top: statusBarHeight,
      })
    },
  },
  methods: {
    goback() {
      wx.navigateBack()
    },
    fanshuHandle() {
      this.setData({
        status: 1,
      })
      setTimeout(() => {
        this.setData({
          showInput: true,
        })
      }, 1500);
    },
    againHandle() {
      this.setData({
        status: 0,
        question: '',
        showInput: false,
        answer: '',
        moreAnswer: '',
      })
    },
    submitHandle() {
      if (!this.data.question) {
        wx.showToast({
          title: '请输入你想知道答案的问题',
          icon: 'none',
        })
        return
      }
      this.setData({
        status: 2,
        showInput: false,
      })
      wx.request({
        url: HOST_URL + 'book/getone',
        method: 'GET',
        data: {
          question: this.data.question,
        },
        success: (res: any) => {
          console.log(res)
          this.setData({
            answer: res.data.data.original,
            moreAnswer: res.data.data.rewrite,
            id: res.data.data.id,
          })
        },
      })
    },
    inputHandle(e: any) {
      console.log(e)
      this.setData({
        question: e.detail.value,
      })
    },
    aijiedu() {
      wx.navigateTo({
        url: '/pages/book-ai/index?id=' + this.data.id + '&wenti=' + this.data.question,
      })
    }
  },
})
